//
//  YMJLyricsLabel.m
//  KTV滚动歌词
//
//  Created by dllo on 16/7/29.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "YMJLyricsLabel.h"

@interface YMJLyricsLabel ()
@property (nonatomic, strong, readwrite) UILabel *textLabel;
@property (nonatomic, strong, readwrite) UILabel *maskLabel;
@property (nonatomic, strong) CALayer *maskLayer;// 用来控制maskLabel渲染的layer
@property (nonatomic, strong) UIColor *maskTextColor;
@property (nonatomic, strong) UIColor *maskBackgroundColor;
@end
@implementation YMJLyricsLabel

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.textLabel];
        [self addSubview:self.maskLabel];
        [self setupDefault];
    }
    return self;
}
- (void)setupDefault {
    self.maskTextColor = [UIColor colorWithRed:0.208 green:0.584 blue:0.333 alpha:1.000];
    self.maskBackgroundColor = [UIColor clearColor];
    self.maskLabel.textColor = _maskTextColor;
    self.maskBackgroundColor = self.maskBackgroundColor;
    CALayer *maskLayer = [CALayer layer];
    maskLayer.anchorPoint = CGPointMake(0, 0.5);// 注意,按默认的anchorPoint,width动画是同时向左右扩展的
    maskLayer.position = CGPointMake(0, CGRectGetHeight(self.bounds) / 2);
    maskLayer.bounds = CGRectMake(0, 0, 0, CGRectGetHeight(self.bounds));
    maskLayer.backgroundColor = [UIColor whiteColor].CGColor;
    self.maskLabel.layer.mask = maskLayer;
    self.maskLayer = maskLayer;
}
- (void)setFont:(UIFont *)font {
    _textLabel.font = font;
    _maskLabel.font = font;
}
- (void)setText:(NSString *)text {
    _textLabel.text = text;
    _maskLabel.text = text;
}
- (void)setTextAlignment:(NSTextAlignment)textAlignment {
    _textLabel.textAlignment = textAlignment;
    _maskLabel.textAlignment = textAlignment;
}
- (void)startLyricsAnimationWithTimeArray:(NSArray *)timeArray andLocatio0nArray:(NSArray *)locationArray {
    CGFloat totalDuration = [timeArray.lastObject floatValue];
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"bounds.size.width"];
    NSMutableArray *keyTimeArray = [NSMutableArray array];
    NSMutableArray *widthArray = [NSMutableArray array];
    for (int i = 0; i < timeArray.count; i++) {
        CGFloat tempArray = [timeArray[i] floatValue] / totalDuration;
        [keyTimeArray addObject:@(tempArray)];
        CGFloat tempWidth = [locationArray[i] floatValue] * CGRectGetWidth(self.bounds);
        [widthArray addObject:@(tempWidth)];
    }
    animation.values = widthArray;
    animation.keyTimes = keyTimeArray;
    animation.duration = totalDuration;
    animation.calculationMode = kCAAnimationLinear;
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    [self.maskLayer addAnimation:animation forKey:@"kLyricsAnimation"];
}
- (void)stopAnimation {
    [self.maskLayer removeAllAnimations];
}
#pragma mark - property
- (UILabel *)textLabel {
    if (!_textLabel) {
        _textLabel = [[UILabel alloc] initWithFrame:self.bounds];
    }
    return _textLabel;
}
- (UILabel *)maskLabel {
    if (!_maskLabel) {
        _maskLabel = [[UILabel alloc] initWithFrame:self.bounds];
    }
    return _maskLabel;
}
@end
